package com.wl156.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Wl156DB extends SQLiteOpenHelper {

	private static final int DATABASE_VERSION = 5;

	private static final String DICTIONARY_TABLE_NAME = "internal_message";
	private static final String DICTIONARY_TABLE_CREATE = "CREATE TABLE "
			+ DICTIONARY_TABLE_NAME + " ( id TEXT UNIQUE, type TEXT, "
			+ "title TEXT, date TEXT, content TEXT, read INTEGER DEFAULT 0, "
			+ "carGoodType TEXT, carOrGoodId TEXT );";

	public Wl156DB(Context context) {
		super(context, "wl156.db", null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(DICTIONARY_TABLE_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS internal_message");
		onCreate(db);
	}

	public static void markMessageRead(Context context, String id) {
		Wl156DB db = new Wl156DB(context);
		db.getWritableDatabase().execSQL(
				"update internal_message set read=1 where id=?",
				new String[] { id });
		db.close();
	}

	public static void changeCarOrGoodFinish(Context context, String type,
			String id) {
		Log.d("wl156db", "type: " + type + ", id: " + id);
		Wl156DB db = new Wl156DB(context);
		db.getWritableDatabase()
				.execSQL(
						"update internal_message set content='已完成' where carGoodType=? and carOrGoodId=?",
						new String[] { type, id });
		db.close();
	}
}